/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.inftel.primeproject.managedBeans;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang3.StringUtils;
import org.inftel.primeproject.model.Users;
import org.inftel.primeproject.remote.UsersFacadeRemote;
import org.primefaces.context.RequestContext;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

/**
 * 
 * @author Administrador
 */
@ManagedBean
@SessionScoped
public class LoginBean {    
    @EJB
    private UsersFacadeRemote userFacade;

    private String email;
    private String password;
    private Users user;
    private Collection<Users> friends;
    private List<Users> friendsList;


    public List<Users> getFriendsList() {
        return friendsList;
    }

    public void setFriendsList(List<Users> friendsList) {
        this.friendsList = friendsList;
    }

    public Collection<Users> getFriends() {
        return friends;
    }

    public void setFriends(Collection<Users> friends) {
        this.friends = friends;
    }
    private boolean loggedIn = false;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Users getUser() {
        return user;
    }

    public void setUser(Users user) {
        this.user = user;
    }
   
    public void login() {        
        RequestContext context = RequestContext.getCurrentInstance();
        FacesMessage msg;
        Users u = userFacade.findByMail(email);        
        if ( (u != null)
                    && (StringUtils.isNotBlank(email)) 
                    && (email.equals(u.getEmail()))
                    && (StringUtils.isNotBlank(password))
                    && password.equals(u.getPassword())) {
                loggedIn = true;  
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Welcome", u.getName());
                FacesContext.getCurrentInstance().addMessage(null, msg);
                context.addCallbackParam("loggedIn", loggedIn);
                this.user = u;    
                this.friends = u.getUsersCollection();
                friends.addAll(u.getUsersCollection1()); 
                friendsList =  new ArrayList<Users>(friends);
          }        
        else{
            loggedIn = false;
            context = RequestContext.getCurrentInstance();   
            msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            context.addCallbackParam("loggedIn", loggedIn);
        }
    }   
    
    public void logout() {
        ExternalContext ctx =
        FacesContext.getCurrentInstance().getExternalContext();
        String ctxPath = ((ServletContext) ctx.getContext()).getContextPath();
        try {
            ((HttpSession) ctx.getSession(false)).invalidate();
            ctx.redirect(ctxPath + "/faces/index.xhtml");
        } catch (IOException ex) {}
    }

    
  
    
     public MapModel getAdvancedModel() {
        MapModel advancedModel = new DefaultMapModel();
        String delims = "[,]";
        String[] tokens = user.getCity().split(delims);
        LatLng mycoord = new LatLng(new Double(tokens[0]),new Double(tokens[1]));
        Marker myMark= new Marker(mycoord,user.getName(),
                user.getName(),
                "http://maps.google.com/mapfiles/ms/micons/red-dot.png");
        advancedModel.addOverlay(myMark);
        return advancedModel;
    }
}

